package com.infomanage.service.plan;

import java.util.List;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import javax.servlet.jsp.JspWriter;

import com.infomanage.authority.AuthorityBean;
import com.infomanage.bean.FieldBean;
import com.infomanage.bean.PaginationBean;
import com.infomanage.bean.SearchBean;
import com.infomanage.dao.PersonDAO;
import com.infomanage.dao.PositionPlanDAO;
import com.infomanage.enums.RoleType;
import com.infomanage.po.Person;
import com.infomanage.po.PositionPlan;
import com.infomanage.po.User;
import com.infomanage.util.Consts;
import com.infomanage.util.StringUtil;
import com.infomanage.vo.PositionPlanItem;
import com.jiuqi.util.ReturnObject;
import com.jiuqi.util.StringUtils;

/**
 * 职位调整计划
 *
 */
public class PositionAdjustPlanService {
	
	public static PositionPlanItem loadPositionPlan(HttpServletRequest request, HttpServletResponse response, JspWriter out){
		try {
			request.setCharacterEncoding("UTF-8");
			String positionId = request.getParameter("positionId");
			if(StringUtils.isEmpty(positionId)) return null;
			PositionPlanDAO dao = new PositionPlanDAO();
			return dao.find(Integer.parseInt(positionId));
		} catch (Exception e) {
			e.printStackTrace();
		}
		return null;
	}
	
	public static List<PositionPlanItem> loadPositionPlanList(HttpServletRequest request, HttpServletResponse response, JspWriter out){
		try {
			request.setCharacterEncoding("UTF-8");
			PositionPlanDAO dao = new PositionPlanDAO();
			List<PositionPlanItem> list = dao.findAll();
			return list;
		} catch (Exception e) {
			e.printStackTrace();
		}
		return null;
	}
	
	public static ReturnObject savePositionPlan(HttpServletRequest request, HttpServletResponse response, JspWriter out){
		ReturnObject ret = new ReturnObject();
		try {
			request.setCharacterEncoding("UTF-8");
			PositionPlan plan = new PositionPlan();
			plan.setPersonId(Integer.parseInt(request.getParameter("personId")));
			PersonDAO personDAO = new PersonDAO();
			Person person = personDAO.find(Person.class, Integer.parseInt(request.getParameter("personId")));
			if(person != null){
				plan.setDeptId(person.getDeptId());
			}
			plan.setPositionBefore(Integer.parseInt(request.getParameter("positionBefore")));
			plan.setPositionAfter(Integer.parseInt(request.getParameter("positionAfter")));
			plan.setTimestamp(request.getParameter("timestamp"));
			PositionPlanDAO dao = new PositionPlanDAO();
			dao.add(plan);
		} catch (Exception e) {
			e.printStackTrace();
			ret.SetErrored(true);
			ret.setErrorMessage("新增失败！");
		}
		return ret;
	}
	
	
	public static ReturnObject modifyPositionPlan(HttpServletRequest request, HttpServletResponse response, JspWriter out){
		ReturnObject ret = new ReturnObject();
		try {
			request.setCharacterEncoding("UTF-8");
			int positionId = Integer.parseInt(request.getParameter("positionId"));
			PositionPlanDAO dao = new PositionPlanDAO();
			PositionPlan plan = dao.find(PositionPlan.class, positionId);
			plan.setPersonId(Integer.parseInt(request.getParameter("personId")));
			PersonDAO personDAO = new PersonDAO();
			Person person = personDAO.find(Person.class, Integer.parseInt(request.getParameter("personId")));
			if(person != null){
				plan.setDeptId(person.getDeptId());
			}
			plan.setPositionBefore(Integer.parseInt(request.getParameter("positionBefore")));
			plan.setPositionAfter(Integer.parseInt(request.getParameter("positionAfter")));
			plan.setTimestamp(request.getParameter("timestamp"));
			dao.modify(plan);
		} catch (Exception e) {
			e.printStackTrace();
			ret.SetErrored(true);
			ret.setErrorMessage("修改失败！");
		}
		return ret;
	}
	
	public static ReturnObject removePositionPlan(HttpServletRequest request, HttpServletResponse response, JspWriter out){
		ReturnObject ret = new ReturnObject();
		try {
			request.setCharacterEncoding("UTF-8");
			String positionId = request.getParameter("positionId");
			PositionPlanDAO dao = new PositionPlanDAO();
			dao.remove(PositionPlan.class, Integer.parseInt(positionId));
		} catch (Exception e) {
			e.printStackTrace();
			ret.SetErrored(true);
			ret.setErrorMessage("删除失败！");
		}
		return ret;
	}
	
	public static List<PositionPlanItem> search(HttpServletRequest request, HttpServletResponse response, JspWriter out){
		try {
			request.setCharacterEncoding("UTF-8");
			PositionPlanDAO dao = new PositionPlanDAO();
			SearchBean searchBean = buildSearchCondition(request, response, out);
			//设置分页
			String currPageNum = request.getParameter("currPageNum");
			PaginationBean page = new PaginationBean();
			if(currPageNum == null){
				page.setCurrentPageNum(1);
			}else{
				page.setCurrentPageNum(Integer.parseInt(currPageNum));
				
			}
			searchBean.setPage(page);
			List<PositionPlanItem> list = dao.findByFields(searchBean);
			return list;
		} catch (Exception e) {
			e.printStackTrace();
		}
		return null;
	}
	
	private static SearchBean buildSearchCondition(HttpServletRequest request, HttpServletResponse response, JspWriter out) throws Exception{
			request.setCharacterEncoding("UTF-8");
			String deptId = request.getParameter("deptId");
			String personId = request.getParameter("personId");
			SearchBean searchBean = new SearchBean();
			if(!StringUtil.isEmpty(deptId)){
				FieldBean field = new FieldBean("deptId", Integer.parseInt(deptId));
				searchBean.add(field);
			}
			if(!StringUtil.isEmpty(personId)){
				FieldBean field = new FieldBean("personId", Integer.parseInt(personId));
				searchBean.add(field);
			}
			
			//设置权限
			User user = (User) request.getSession().getAttribute(Consts.LOGINED_USER);
			if(user != null){
				AuthorityBean authority = new AuthorityBean();
				authority.setRole(RoleType.valueOf(user.getRoleId()));
				authority.setDeptId(user.getAssociatedDept());
				searchBean.setAuthority(authority);
			}
		return searchBean;
	}
	
	public static int getPageCount(HttpServletRequest request, HttpServletResponse response, JspWriter out){
		try {
			PositionPlanDAO dao = new PositionPlanDAO();
			SearchBean searchBean = buildSearchCondition(request, response, out);
			return (dao.count(searchBean) + PaginationBean.pageSize - 1)/PaginationBean.pageSize;
		} catch (Exception e) {
			e.printStackTrace();
		}
		return 1;
	}
	
	public static ReturnObject removeBatchPositionPlan(HttpServletRequest request, HttpServletResponse response, JspWriter out){
		ReturnObject ret = new ReturnObject();
		try {
			request.setCharacterEncoding("UTF-8");
			String batchIds = request.getParameter("batchIds");
			PositionPlanDAO dao = new PositionPlanDAO();
			if(batchIds != null){
				String[] ids = batchIds.split(";");
				for(String id : ids){
					dao.remove(PositionPlan.class, Integer.parseInt(id));
				}
			}
		} catch (Exception e) {
			e.printStackTrace();
			ret.SetErrored(true);
			ret.setErrorMessage("删除失败！");
		}
		return ret;
	}
}
